<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Dianqi;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Renderable\DeptTable;
use App\Models\Dept;

class DianqiController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Dianqi(), function (Grid $grid) {

            $grid->quickSearch('dqname', 'dept.deptname')->placeholder('输入部门或电器名搜索...');
            // 禁用过滤器按钮
            $grid->disableFilterButton();
            // 禁用刷新按钮
            $grid->disableRefreshButton();
            // 禁用行选择器
            // $grid->disableRowSelector();

            $grid->export();

            $grid->model()->orderBy('id', 'desc');

            $grid->column('id')->sortable();

            $grid->model()->with(['dept']);
            // $grid->column('id')->sortable();
            $grid->column('dqid');
            $grid->column('dqname');
            $grid->column('dept.deptname', '部门名称')->link(function ($value) {
                if ($value) {
                    $deptid = Dept::where('deptname', $value)->first()->id;
                    return admin_url('depts/' . $deptid);
                }
            });

            $grid->column('zrrname');

            $grid->column('dqgl');
            $grid->column('shuliang');
            $grid->column('weizhi');
            $grid->column('didian');
            $grid->column('yongtu');
            $grid->column('shijian');
            // $grid->column('created_at');
            // $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $repository = new Dianqi(['dept']);
        return Show::make($id, $repository, function (Show $show) {
            $show->field('id');
            $show->field('dept.deptname', '部门/科室');
            $show->field('dqid');
            $show->field('zrrname');
            $show->field('dqname');
            $show->field('dqgl');
            $show->field('shuliang');
            $show->field('weizhi');
            $show->field('didian');
            $show->field('yongtu');
            $show->field('shijian');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Dianqi(), function (Form $form) {
            $form->display('id');
            // $form->text('dept_id');

            $form->selectTable('dept_id', '所属部门：')
                ->title('部门列表')
                ->from(DeptTable::make())
                ->model(Dept::class, 'id', 'deptname')->required();

            $form->text('dqid')->placeholder('DQ-楼号-层号-顺序号，例如：DQ-2-11-5，2号楼11层第5个')->required();
            $form->text('zrrname')->required();
            $form->text('dqname')->required();
            $form->text('dqgl')->placeholder('单位w')->required();
            $form->number('shuliang')->required();
            $form->text('weizhi')->placeholder('例如：2号楼11层')->required();
            $form->text('didian')->placeholder('例如：办公室')->required();
            $form->text('yongtu')->placeholder('例如：热饭')->required();
            $form->text('shijian')->placeholder('例如：全天')->required();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
